
#if !defined(AFX_CWAMANAGER_H__2012__INCLUDED_)
#define AFX_CWAMANAGER_H__2012__INCLUDED_

#include <vector>
#include "../Inc/bt_ui.h"
#include "../Frame/CustomMsgCenter.h"

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#define WA_ERROR_NONE				0x00
#define WA_ERROR_SUCCESS			0x01
#define WA_ERROR_TIMEOUT			0x02
#define WA_ERROR_NOTCONNECT			0x03

#define GY_WA_NAME1			"HZGY-GYWA-I-"
#define GY_WA_NAME2			"linvor"

#define BLUESOLEIL_REG_KEY_PATH				_T("SOFTWARE\\IVT Corporation\\BlueSoleil\\Install")
#define BLUESOLEIL_REG_KEY_NAME				_T("InstallDir")
#define BLUESOLEIL_EXEFILE					_T("BlueSoleil.exe")

typedef std::vector<BLUETOOTH_DEVICE_INFO_EX>	BT_DEV_INFO_VECTOR;

typedef struct _tagWABasicPara
{
	DWORD		dwSize;
	DWORD		dwOpenThr;
	DWORD		dwCloseThr;
	DWORD		dwStiUpTime;
	DWORD		dwStiDownTime;
	DWORD		dwPulseFrq;
	DWORD		dwPulseWidth;
	DWORD		dwPulseCount;
	DWORD		dwWeek;
	DWORD		dwYear;
	DWORD		dwMonth;
	DWORD		dwDay;
	DWORD		dwHour;
	DWORD		dwMinute;
}WA_BASIC_PARA;

typedef struct _tagWAHistoryData
{
	DWORD			dwSize;
	CTime			tTime;
	DWORD			dwTimespan;
	DWORD			dwStepCount;
	DWORD			dwMRNCode;
}WA_HISTORY_DATA;

class CWADevice;
typedef std::vector<CWADevice*> VEC_WADEVICE;

class CWAManager : public CCustMsgBase
{
public:
	CWAManager();
	~CWAManager();


	//////////////////////////////////////////////////////////////////////////
	DWORD				cmdReadCommVersion	(int nDevIndex);
	CString				cmdReadFirmwareVer	(int nDevIndex);
	BOOL				cmdConnectWA		(int nDevIndex);
	BOOL				cmdDisconnectWA		(int nDevIndex);
	CString				cmdReadWADevID		(int nDevIndex);
	DWORD				cmdGetBattery		(int nDevIndex);
	BOOL				cmdStartReadGait	(int nDevIndex);
	BOOL				cmdStopReadGait		(int nDevIndex);
	DWORD				cmdReadGaitValue	(int nDevIndex);
	WA_BASIC_PARA		cmdGetBasicPara		(int nDevIndex);
	BOOL				cmdWriteBasicPara	(int nDevIndex, WA_BASIC_PARA wbp);
	BOOL				cmdWriteParamOCD	(int nDevIndex, WA_BASIC_PARA wbpOpenCloseDown);
	DWORD				cmdReadMRNCode		(int nDevIndex);
	BOOL				cmdWriteMRNCode		(int nDevIndex,	DWORD dwMRNCode);
	DWORD				cmdWriteStiDownTime	(int nDevIndex, DWORD dwDownTime);
	BOOL				cmdWriteStiUpTime	(int nDevIndex, DWORD dwUpTime);
	BOOL				cmdWriteTriggerClose(int nDevIndex, DWORD dwClose);
	BOOL				cmdWriteTriggerOpen	(int nDevIndex, DWORD dwOpen);
	BOOL				cmdWritePulseFreq	(int nDevIndex, DWORD dwPulseFreq);
	BOOL				cmdWritePulseWidth	(int nDevIndex, DWORD dwPulseWidth);
	BOOL				cmdWritePulseCount	(int nDevIndex, DWORD dwPulseCount);
	BOOL				cmdStartReadHistory	(int nDevIndex);
	WA_HISTORY_DATA		cmdReadHistoryData	(int nDevIndex);
	BOOL				cmdClearHistoryData	(int nDevIndex);
	BOOL				cmdWriteWATime		(int nDevIndex, WA_BASIC_PARA wbp);
	//////////////////////////////////////////////////////////////////////////

	BOOL				Init();
	BOOL				Uninit();

	BOOL				LaunchBlueSoleil();
	BOOL				IsWAConnected(int nDevIndex);
	BOOL				IsAllWAConnected();
	int					GetWADeviceCount();

	CString				GetWADevName(int nDevIndex);
	DWORD				GetWACommVersion(int nDevIndex);
	CString				GetWAAddress(int nDevIndex);
	CString				GetWADevID(int nDevIndex);
		
	int					GetFirstDevIndex();
	int					GetNextDevIndex(int nDevIndex);

	DWORD				GetLastWAError(int nDevIndex);

	BOOL				RemoveDeviceFromList(int nDevIndex);

	BOOL				EnterBTCS();
	BOOL				LeaveBTCS();

	BOOL				WaitThreadsQuit();

	void				SearchWAImmediately(){OnTimerInquireWA(NULL, NULL);}

private:
	BOOL				InquireWA();
	BOOL				InitBluetooth();

	BOOL				IsGYWADevice(char* pDevInfoEx);
	BOOL				VerifyBTDevice(BT_DEV_INFO_VECTOR* pVecDev, BYTE* pBTDevAddress);

	BOOL				AddWADevice(BLUETOOTH_DEVICE_INFO_EX* pDevInfoEx);
	BOOL				UpdateDevList(std::vector<BLUETOOTH_DEVICE_INFO_EX>* pLatestDevList);
	static UINT	WINAPI	InquireBTDeviceThread(LPVOID lpParam);

	static void			GeneralError(DWORD dwErrorCode);
	static void			BTConnectCallback(WORD wServiceClass, BYTE* lpBdAddr, UCHAR ucStatus, DWORD dwConnetionHandle);

	void				OnWAConnected(WPARAM wParam, LPARAM lParam);
	void				OnTimerInquireWA(WPARAM wParam, LPARAM lParam);
	void				OnWAConnectBTFail(WPARAM wParam, LPARAM lParam);
		
	DWORD				GetBCDValue(BYTE byteData);
	BYTE				GetBCDByte(DWORD dwData);

private:
	VEC_WADEVICE		m_vecWADevice;

	HANDLE				m_hInquireThread;
	HANDLE				m_hEventInquireWA;
	BOOL				m_bInquireTimerStarted;
	volatile BOOL		m_bInquiringWA;

	CRITICAL_SECTION	m_csBluetoothToken;
	CRITICAL_SECTION	m_csVectorDevList;
};


#endif // !defined(AFX_CWAMANAGER_H__2012__INCLUDED_
